﻿using System;
using org.se.fs.presenter.IView.Profile;
using org.se.fs.presenter.Library;
using org.se.fs.presenter.Task.Template;

namespace org.se.fs.presenter.View.Profile
{
    public class BranchSubMenu : Template.SubMenu
    {
        private readonly IBranchSubMenu _view;

        public BranchSubMenu(IBranchSubMenu view) : this(view, new Task.Context.Branch()) { }

        public BranchSubMenu(IBranchSubMenu view, Crud task)
        {
            _view = view;
            Task = task;
            FormView = _view;
            Name = "Branch";
        }

        protected override void SetUi()
        {
            _view.FullName = Data["name"].ToString();
            _view.Code = Data["code"].ToString();
            _view.Address = Data["address"].ToString();
            _view.Fax = Data["fax_num"].ToString();
            _view.Email = Data["email"].ToString();
            _view.Mobile = Data["mobile_num"].ToString();
            _view.Tel = Data["tel_num"].ToString();
        }

        protected override void FillData()
        {
            Data["name"] = _view.FullName;
            Data["code"] = _view.Code;
            Data["address"] = _view.Address;
            Data["email"] = _view.Email;
            Data["fax_num"] = _view.Fax;
            Data["mobile_num"] = _view.Mobile;
            Data["tel_num"] = _view.Tel;
            Data["user_create_id"] = ToUInt64(Config.User["id"]);
        }

        protected override bool Validate()
        {
            if (string.IsNullOrEmpty(_view.FullName.Trim()))
            {
                TriggerValidate(new FormEvent(false) { Exception = new Exception("Invalid empty name.") });
                _view.FocusName();
                return false;
            }

            if (string.IsNullOrEmpty(_view.Code.Trim()))
            {
                TriggerValidate(new FormEvent(false) { Exception = new Exception("Invalid empty code.") });
                _view.FocusCode();
                return false;
            }

            if (string.IsNullOrEmpty(_view.Address.Trim()))
            {
                TriggerValidate(new FormEvent(false) { Exception = new Exception("Invalid empty address.") });
                _view.FocusAddress();
                return false;
            }

            TriggerValidate(new FormEvent(true));
            return true;
        }
    }
}
